﻿using MyProduce.enumeration;
using MyProduce.MyProduce.ProduceDto;
using MyProduce.Produce;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyProduce.MyProduce
{
    public class SeedbirthAppService : ApplicationService, ISeedbirthAppService
    {
        private readonly IRepository<Seedbirth,int> _seedbirthRepository;
        public SeedbirthAppService(IRepository<Seedbirth, int> seedbirthRepository)
        {
            _seedbirthRepository = seedbirthRepository;
          
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Aid"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int Aid)
        {
           await _seedbirthRepository.DeleteAsync(Aid);
        }
        /// <summary>
        /// 显示分娩难易
        /// </summary>
        /// <returns></returns>

        public List<object> GetDifficultyEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<Difficulty>())
            {
                list.Add(new { value = (int)item, name = item.ToString() });
            }
            return list.ToList();
        }
        public List<object> GetmaternityEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<maternity>())
            {
                list.Add(new { value = (int)item, name = item.ToString() });
            }
            return list.ToList();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="eid"></param>
        /// <returns></returns>

        public  async Task<SeedbirthDto> GetPrentalFindaa(int eid)
        {
            var result = await _seedbirthRepository.GetAsync(eid);
            return ObjectMapper.Map<Seedbirth, SeedbirthDto>(result);
        }

        /// <summary>
        /// 显示分娩信息
        /// </summary>
        /// <returns></returns>
        public  async Task<List<SeedbirthDto>> GetSeedbirthShow()
        {
            var result = await _seedbirthRepository.GetListAsync();
            return ObjectMapper.Map<List<Seedbirth>, List<SeedbirthDto>>(result);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="eid"></param>
        /// <returns></returns>

        public async Task<SeedbirthDto> GetSheepdbirFindaa(int eid)
        {
            var result= await _seedbirthRepository.GetAsync(eid);
            return ObjectMapper.Map<Seedbirth, SeedbirthDto>(result);
        }
        /// <summary>
        /// 显示肥羊批次
        /// </summary>
        /// <returns></returns>
        public List<object> GetsheeplotyEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<sheeplot>())
            {
                list.Add(new { value = (int)item, name = item.ToString() });
            }
            return list.ToList();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="appearanceDTO"></param>
        /// <returns></returns>
        public  async Task<EarResult<SeedbirthDto>> SeedbirAdds(SeedbirthDto appearanceDTO)
        {
            Seedbirth seedbirth=new Seedbirth();
            seedbirth.Transfertobuilding=appearanceDTO.Transfertobuilding;
            seedbirth.Variety = appearanceDTO.Variety;
            seedbirth.Cottage = appearanceDTO.Cottage;
            seedbirth.Field = appearanceDTO.Field;
            seedbirth.Overbit = appearanceDTO.Overbit;
            seedbirth.Drivefield = appearanceDTO.Drivefield;
            seedbirth.Surrenderdate = appearanceDTO.Surrenderdate;
            seedbirth.RoundType = appearanceDTO.RoundType;
            seedbirth.Breedingpattern = appearanceDTO.Breedingpattern;
            seedbirth.Speciesestrustype = appearanceDTO.Speciesestrustype;
            seedbirth.BreedfemaleRAMS = appearanceDTO.BreedfemaleRAMS;
            seedbirth.Dateofbreeding = appearanceDTO.Dateofbreeding;
            seedbirth.principal = appearanceDTO.principal;
            seedbirth.Gestationdays = appearanceDTO.Gestationdays;
            seedbirth.Difficultyofdelivery = appearanceDTO.Difficultyofdelivery;
            seedbirth.Fatsheeplot = appearanceDTO.Fatsheeplot;
            seedbirth.Maternity = appearanceDTO.Maternity;
            seedbirth.lactation = appearanceDTO.lactation;
            seedbirth.Mastitis = appearanceDTO.Mastitis;
            seedbirth.Broodnumber = appearanceDTO.Broodnumber;
            seedbirth.Healthynumber = appearanceDTO.Healthynumber;
            seedbirth.Weaklambnumber = appearanceDTO.Weaklambnumber;
            seedbirth.Stillbirths = appearanceDTO.Stillbirths;
            seedbirth.Mummynumber = appearanceDTO.Mummynumber;
            seedbirth.Numberoffreaks = appearanceDTO.Numberoffreaks;
            seedbirth.Livelambs = appearanceDTO.Livelambs;
            seedbirth.Sex = appearanceDTO.Sex;
            var result = await _seedbirthRepository.InsertAsync(seedbirth);
            var res = ObjectMapper.Map<Seedbirth, SeedbirthDto>(result);
            if (res != null)
            {
                return new EarResult<SeedbirthDto>() { Code = 200, Message = "添加成功" };
            }
            else
            {
                return new EarResult<SeedbirthDto>() { Code = 500,  Message = "添加失败" };
            }
        }

        public async Task<EarResult<SeedbirthDto>> UpdateAsync(SeedbirthDto dto)
        {
            var res = await  _seedbirthRepository.FindAsync(dto.Id);
            if (res == null)
            {
                throw new UserFriendlyException("没有找到对应的用户");
            }
            ObjectMapper.Map(dto, res);
            var count = await _seedbirthRepository.UpdateAsync(res);
            var result = ObjectMapper.Map<Seedbirth, SeedbirthDto>(count);

            if (result != null)
            {
                return new EarResult<SeedbirthDto>() { Code = 200, Message = "修改成功！" };
            }
            else
            {
                return new EarResult<SeedbirthDto>() { Code = 500, Message = "修改失败！" };
            }
        }
        /// <summary>
        /// 显示活羊
        /// </summary>
        /// <returns></returns>
        public async Task<List<livelamberDto>> GetlivelamberShow()
        {
            var result = await _seedbirthRepository.GetListAsync();
          
            var list = (from a in result
                        
                        select new livelamberDto
                        {
                           Num=a.Livelambs,

                        }).ToList();
          

            return list;
        }
        /// <summary>
        /// 显示死羊
        /// </summary>
        /// <returns></returns>
        public async Task<List<livelamberDto>> GetliffreksShow()
        {
            var result = await _seedbirthRepository.GetListAsync();

            var list = (from a in result

                        select new livelamberDto
                        {
                            Num = a.Numberoffreaks,

                        }).ToList();


            return list;
        }
    }
}
